package cn.ibizlab.odoo.core.odoo_base.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ires_currency;
import cn.ibizlab.odoo.core.odoo_base.domain.Res_currency;
import cn.ibizlab.odoo.core.odoo_base.filter.Res_currencySearchContext;
import cn.ibizlab.odoo.core.odoo_base.service.IRes_currencyService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[币种] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Res_currencyServiceImpl implements IRes_currencyService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ires_currencyClientService res_currencyClientService;

    @Override
    public Res_currency get(Integer id) {
        Ires_currency clientModel = res_currencyClientService.createModel();
        clientModel.setId(id);
        res_currencyClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Res_currency get(Res_currency res_currency) {
        Res_currency retres_currency = this.get(res_currency.getId()) ;
    	BeanCopier copier=BeanCopier.create(retres_currency.getClass(), res_currency.getClass(), false);
        copier.copy(retres_currency, res_currency, null);
        return res_currency ;
    }

    @Override
    public boolean update(Res_currency res_currency) {
        boolean bOk = false ;
        Ires_currency clientModel = this.convert2Model(res_currency,null);
        res_currencyClientService.update(clientModel) ;
        this.convert2Domain(clientModel,res_currency) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ires_currency clientModel = res_currencyClientService.createModel();
        clientModel.setId(id);
        res_currencyClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Res_currency> res_currencys, int batchSize) {

                        //res_currencyClientService.createBatch(res_currencys,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //res_currencyClientService.removeBatch(res_currencys) ;

    }

    @Override
    public boolean create(Res_currency res_currency) {
        boolean bOk = false ;
        Ires_currency clientModel = this.convert2Model(res_currency,null);
        res_currencyClientService.create(clientModel) ;
        this.convert2Domain(clientModel,res_currency);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Res_currency> res_currencys, int batchSize) {

        //res_currencyClientService.updateBatch(res_currencys,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Res_currency> searchDefault(Res_currencySearchContext context) {
        List<Res_currency> list = new ArrayList<Res_currency>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_currency> clientModelList = res_currencyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_currency clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_currency>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Res_currency> selectByOdoo_create(Integer id){
        Res_currencySearchContext context = new Res_currencySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_currency> list = new ArrayList<Res_currency>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_currency> clientModelList = res_currencyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_currency clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_currency>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Res_currency> selectByOdoo_write(Integer id){
        Res_currencySearchContext context = new Res_currencySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_currency> list = new ArrayList<Res_currency>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_currency> clientModelList = res_currencyClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_currency clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_currency>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ires_currency convert2Model(Res_currency domain , Ires_currency model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = res_currencyClientService.createModel() ;
        }

         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getDecimal_placesDirtyFlag())
            model.setDecimal_places(domain.getDecimal_places());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCurrency_unit_labelDirtyFlag())
            model.setCurrency_unit_label(domain.getCurrency_unit_label());
         if(domain.getRoundingDirtyFlag())
            model.setRounding(domain.getRounding());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.getPositionDirtyFlag())
            model.setPosition(domain.getPosition());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getRateDirtyFlag())
            model.setRate(domain.getRate());
         if(domain.getRate_idsDirtyFlag())
            model.setRate_ids(domain.getRate_ids());
         if(domain.getSymbolDirtyFlag())
            model.setSymbol(domain.getSymbol());
         if(domain.getCurrency_subunit_labelDirtyFlag())
            model.setCurrency_subunit_label(domain.getCurrency_subunit_label());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Res_currency convert2Domain( Ires_currency model ,Res_currency domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Res_currency();
        }

        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getDecimal_placesDirtyFlag())
            domain.setDecimal_places(model.getDecimal_places());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCurrency_unit_labelDirtyFlag())
            domain.setCurrency_unit_label(model.getCurrency_unit_label());
        if(model.getRoundingDirtyFlag())
            domain.setRounding(model.getRounding());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.getPositionDirtyFlag())
            domain.setPosition(model.getPosition());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getRateDirtyFlag())
            domain.setRate(model.getRate());
        if(model.getRate_idsDirtyFlag())
            domain.setRate_ids(model.getRate_ids());
        if(model.getSymbolDirtyFlag())
            domain.setSymbol(model.getSymbol());
        if(model.getCurrency_subunit_labelDirtyFlag())
            domain.setCurrency_subunit_label(model.getCurrency_subunit_label());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
